package com.szzg10.thinking.chapter18;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class UsingRandomAccessFile {

	public static void main(String[] args) throws IOException {
		RandomAccessFile out = new RandomAccessFile(BasicFileOutput.FILE_NAME_OUT, "rw");
		for(int i = 0; i < 7; i++) {
			out.writeDouble(1.414 * i);
		}
		out.writeUTF("The end of the file");
		out.close();
		
		display();
		System.out.println();
		
		out = new RandomAccessFile(BasicFileOutput.FILE_NAME_OUT, "rw");
		out.seek(8 * 5);
		out.writeDouble(47.0001);
		out.close();
		
		display();
	}
	
	private static void display() throws IOException {
		RandomAccessFile in = new RandomAccessFile(BasicFileOutput.FILE_NAME_OUT, "r");
		for(int i = 0; i < 7; i++) {
			System.out.println(i + ": " + in.readDouble());
		}
		in.readUTF();
		in.close();
	}
}
